<!DOCTYPE html>
<html lang=zh>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="原文链接：https:&#x2F;&#x2F;juejin.im&#x2F;post&#x2F;5c9e23e36fb9a05e58493c50?utm_source&#x3D;gold_browser_extension  一、redis简介1、概述  Redis 是一个高性能的key-value数据库。 redis的出现，很大程度补偿了memcached这类key&#x2F;value存储的不足，在部 分场合可以对关系数据库起到很好的补充作用。它提供">
<meta property="og:type" content="article">
<meta property="og:title" content="SpringBoot+Redis作为二级缓存整合">
<meta property="og:url" content="https:&#x2F;&#x2F;fengzxia.gitee.io&#x2F;posts&#x2F;90da3b9e.html">
<meta property="og:site_name" content="只是学习学习的博客">
<meta property="og:description" content="原文链接：https:&#x2F;&#x2F;juejin.im&#x2F;post&#x2F;5c9e23e36fb9a05e58493c50?utm_source&#x3D;gold_browser_extension  一、redis简介1、概述  Redis 是一个高性能的key-value数据库。 redis的出现，很大程度补偿了memcached这类key&#x2F;value存储的不足，在部 分场合可以对关系数据库起到很好的补充作用。它提供">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https:&#x2F;&#x2F;imgconvert.csdnimg.cn&#x2F;aHR0cHM6Ly9pLmxvbGkubmV0LzIwMTkvMDQvMTEvNWNhZWFkNDU3ZjFhNi5wbmc">
<meta property="og:image" content="https:&#x2F;&#x2F;imgconvert.csdnimg.cn&#x2F;aHR0cHM6Ly9pLmxvbGkubmV0LzIwMTkvMDQvMTEvNWNhZWNkNTMwM2IwNy5wbmc">
<meta property="article:published_time" content="2019-04-11T06:48:04.000Z">
<meta property="article:modified_time" content="2020-04-17T10:28:04.411Z">
<meta property="article:author" content="只是学习学习">
<meta property="article:tag" content="二级缓存">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https:&#x2F;&#x2F;imgconvert.csdnimg.cn&#x2F;aHR0cHM6Ly9pLmxvbGkubmV0LzIwMTkvMDQvMTEvNWNhZWFkNDU3ZjFhNi5wbmc">
    
    
        
          
              <link rel="shortcut icon" href="/images/favicon.ico">
          
        
        
          
            <link rel="icon" type="image/png" href="/images/favicon-192x192.png" sizes="192x192">
          
        
        
          
            <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
          
        
    
    <!-- title -->
    <title>SpringBoot+Redis作为二级缓存整合</title>
    <!-- styles -->
    
<link rel="stylesheet" href="/css/style.css">

    <!-- persian styles -->
    
      
<link rel="stylesheet" href="/css/rtl.css">

    
    <!-- rss -->
    
    
    
<meta name="generator" content="Hexo 4.1.1"></head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
         
          <li><a href="/">主页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/categories/">分类</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/about/">关于</a></li>
         
          <li><a href="/friend_ship/">友链</a></li>
        
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" href="/posts/f6f65c4b.html"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" href="/posts/c83b5e46.html"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">上一篇</span>
      <span id="i-next" class="info" style="display:none;">下一篇</span>
      <span id="i-top" class="info" style="display:none;">返回顶部</span>
      <span id="i-share" class="info" style="display:none;">分享文章</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://fengzxia.gitee.io/posts/90da3b9e.html" target="_blank" rel="noopener"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://fengzxia.gitee.io/posts/90da3b9e.html&text=SpringBoot+Redis作为二级缓存整合" target="_blank" rel="noopener"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://fengzxia.gitee.io/posts/90da3b9e.html&title=SpringBoot+Redis作为二级缓存整合" target="_blank" rel="noopener"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://fengzxia.gitee.io/posts/90da3b9e.html&is_video=false&description=SpringBoot+Redis作为二级缓存整合" target="_blank" rel="noopener"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=SpringBoot+Redis作为二级缓存整合&body=Check out this article: https://fengzxia.gitee.io/posts/90da3b9e.html"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=https://fengzxia.gitee.io/posts/90da3b9e.html&title=SpringBoot+Redis作为二级缓存整合" target="_blank" rel="noopener"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://fengzxia.gitee.io/posts/90da3b9e.html&title=SpringBoot+Redis作为二级缓存整合" target="_blank" rel="noopener"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://fengzxia.gitee.io/posts/90da3b9e.html&title=SpringBoot+Redis作为二级缓存整合" target="_blank" rel="noopener"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://fengzxia.gitee.io/posts/90da3b9e.html&title=SpringBoot+Redis作为二级缓存整合" target="_blank" rel="noopener"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://fengzxia.gitee.io/posts/90da3b9e.html&name=SpringBoot+Redis作为二级缓存整合&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=https://fengzxia.gitee.io/posts/90da3b9e.html&t=SpringBoot+Redis作为二级缓存整合" target="_blank" rel="noopener"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一、redis简介"><span class="toc-number">1.</span> <span class="toc-text">一、redis简介</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1、概述"><span class="toc-number">1.1.</span> <span class="toc-text">1、概述</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2、优点"><span class="toc-number">1.2.</span> <span class="toc-text">2、优点</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二、redis的搭建"><span class="toc-number">2.</span> <span class="toc-text">二、redis的搭建</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1、安装"><span class="toc-number">2.1.</span> <span class="toc-text">1、安装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2、测试"><span class="toc-number">2.2.</span> <span class="toc-text">2、测试</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#三、基本环境配置"><span class="toc-number">3.</span> <span class="toc-text">三、基本环境配置</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1、pom依赖"><span class="toc-number">3.0.1.</span> <span class="toc-text">1、pom依赖</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2、yml配置"><span class="toc-number">3.0.2.</span> <span class="toc-text">2、yml配置</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3、测试连接"><span class="toc-number">3.0.3.</span> <span class="toc-text">3、测试连接</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#四、三个基本缓存注解"><span class="toc-number">4.</span> <span class="toc-text">四、三个基本缓存注解</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1、-Cacheable"><span class="toc-number">4.0.1.</span> <span class="toc-text">1、@Cacheable</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2、-CachePut"><span class="toc-number">4.0.2.</span> <span class="toc-text">2、@CachePut</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3、-CacheEvict"><span class="toc-number">4.0.3.</span> <span class="toc-text">3、@CacheEvict</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#三者共有属性"><span class="toc-number">4.0.4.</span> <span class="toc-text">三者共有属性</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#CacheEvict的allEntries和beforeInvocation属性"><span class="toc-number">4.0.5.</span> <span class="toc-text">@CacheEvict的allEntries和beforeInvocation属性</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#实例"><span class="toc-number">4.0.6.</span> <span class="toc-text">实例</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#五、demo地址"><span class="toc-number">5.</span> <span class="toc-text">五、demo地址</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#注意"><span class="toc-number">5.1.</span> <span class="toc-text">注意</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#六、CacheManager的定制"><span class="toc-number">6.</span> <span class="toc-text">六、CacheManager的定制</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1、未定制前"><span class="toc-number">6.1.</span> <span class="toc-text">1、未定制前</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2、进行定制"><span class="toc-number">6.2.</span> <span class="toc-text">2、进行定制</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#配置类"><span class="toc-number">6.2.1.</span> <span class="toc-text">配置类</span></a></li></ol></li></ol></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        SpringBoot+Redis作为二级缓存整合
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">只是学习学习的博客</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2019-04-11T06:48:04.000Z" itemprop="datePublished">2019-04-11</time>
        
      
    </div>


      
    <div class="article-category">
        <i class="fas fa-archive"></i>
        <a class="category-link" href="/categories/Java/">Java</a> › <a class="category-link" href="/categories/Java/Redis/">Redis</a> › <a class="category-link" href="/categories/Java/Spring/">Spring</a> › <a class="category-link" href="/categories/Java/Spring/SpringBoot/">SpringBoot</a> › <a class="category-link" href="/categories/%E8%BD%AC%E8%BD%BD/">转载</a>
    </div>


      
    <div class="article-tag">
        <i class="fas fa-tag"></i>
        <a class="tag-link" href="/tags/%E4%BA%8C%E7%BA%A7%E7%BC%93%E5%AD%98/" rel="tag">二级缓存</a>
    </div>


    </div>
  </header>
  

  <div class="content"itemprop ="articleBody">
    <blockquote>
<p>原文链接：<a href="https://juejin.im/post/5c9e23e36fb9a05e58493c50?utm_source=gold_browser_extension" target="_blank" rel="noopener">https://juejin.im/post/5c9e23e36fb9a05e58493c50?utm_source=gold_browser_extension</a></p>
</blockquote>
<h2 id="一、redis简介"><a href="#一、redis简介" class="headerlink" title="一、redis简介"></a>一、redis简介</h2><h3 id="1、概述"><a href="#1、概述" class="headerlink" title="1、概述"></a>1、概述</h3><p>  Redis 是一个高性能的key-value数据库。 redis的出现，很大程度补偿了memcached这类key/value存储的不足，在部 分场合可以对关系数据库起到很好的补充作用。它提供了Java，C/C++，C#，PHP，JavaScript，Perl，Object-C，Python，Ruby，Erlang等客户端，使用很方便。</p>
<h3 id="2、优点"><a href="#2、优点" class="headerlink" title="2、优点"></a>2、优点</h3><ol>
<li>数据操作全在内存，读写性能强。</li>
<li>数据结构丰富，支持string，hash，list，set及zset(sorted set)。</li>
<li>支持主从复制，以及数据持久化等<h2 id="二、redis的搭建"><a href="#二、redis的搭建" class="headerlink" title="二、redis的搭建"></a>二、redis的搭建</h2><h3 id="1、安装"><a href="#1、安装" class="headerlink" title="1、安装"></a>1、安装</h3>按顺序执行如下命令:<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">wget http:&#x2F;&#x2F;download.redis.io&#x2F;releases&#x2F;redis-5.0.4.tar.gz</span><br><span class="line">tar xzf redis-5.0.4.tar.gz</span><br><span class="line">cd redis-5.0.4</span><br><span class="line">make</span><br><span class="line">make install</span><br></pre></td></tr></table></figure>
<h3 id="2、测试"><a href="#2、测试" class="headerlink" title="2、测试"></a>2、测试</h3>开启服务<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-server</span><br></pre></td></tr></table></figure>
启动客户机交互测试，默认6379端口，没有密码<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">redis-cli -p 6379</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; set k1 k2</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; get k1</span><br><span class="line">&quot;k2&quot;</span><br><span class="line">127.0.0.1:6379&gt;</span><br></pre></td></tr></table></figure>

</li>
</ol>
<h2 id="三、基本环境配置"><a href="#三、基本环境配置" class="headerlink" title="三、基本环境配置"></a>三、基本环境配置</h2><h4 id="1、pom依赖"><a href="#1、pom依赖" class="headerlink" title="1、pom依赖"></a>1、pom依赖</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">properties</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--指定jdk版本--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">java.version</span>&gt;</span>1.8<span class="tag">&lt;/<span class="name">java.version</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--统一编码格式--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">project.build.sourceEncoding</span>&gt;</span>UTF-8<span class="tag">&lt;/<span class="name">project.build.sourceEncoding</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">mybatis.version</span>&gt;</span>2.0.0<span class="tag">&lt;/<span class="name">mybatis.version</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">properties</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-cache<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-data-redis<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-thymeleaf<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.mybatis.spring.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">version</span>&gt;</span>$&#123;mybatis.version&#125;<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">scope</span>&gt;</span>runtime<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-test<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.projectlombok<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>lombok<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="2、yml配置"><a href="#2、yml配置" class="headerlink" title="2、yml配置"></a>2、yml配置</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">server:</span><br><span class="line">  port: 80</span><br><span class="line">spring:</span><br><span class="line">  http:</span><br><span class="line">    encoding:</span><br><span class="line">      charset: UTF-8</span><br><span class="line">  cache:</span><br><span class="line">    type: redis</span><br><span class="line">  redis:</span><br><span class="line">    host: 127.0.0.1</span><br><span class="line">    port: 9455</span><br><span class="line">    password: 123456</span><br><span class="line">  datasource:</span><br><span class="line">    driver-class-name: com.mysql.jdbc.Driver</span><br><span class="line">    username: root</span><br><span class="line">    password: root</span><br><span class="line">    url: jdbc:mysql:&#x2F;&#x2F;127.0.0.1:3306&#x2F;redis-cache?useSSL&#x3D;false</span><br><span class="line">logging:</span><br><span class="line">  level:</span><br><span class="line">    com.lx: debug</span><br></pre></td></tr></table></figure>

<h4 id="3、测试连接"><a href="#3、测试连接" class="headerlink" title="3、测试连接"></a>3、测试连接</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.lx;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"><span class="keyword">import</span> org.junit.runner.RunWith;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.test.context.SpringBootTest;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.redis.core.StringRedisTemplate;</span><br><span class="line"><span class="keyword">import</span> org.springframework.test.context.junit4.SpringRunner;</span><br><span class="line"></span><br><span class="line"><span class="meta">@RunWith</span>(SpringRunner<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line"><span class="class">@<span class="title">SpringBootTest</span></span></span><br><span class="line"><span class="class"><span class="title">public</span> <span class="title">class</span> <span class="title">RediscacheApplicationTest</span> </span>&#123;</span><br><span class="line">  <span class="meta">@Autowired</span></span><br><span class="line">  <span class="keyword">private</span> StringRedisTemplate stringRedisTemplate;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Test</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">contextLoads</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    stringRedisTemplate.opsForValue().set(<span class="string">"k1"</span>, <span class="string">"v1"</span>);</span><br><span class="line">    System.out.println(stringRedisTemplate.opsForValue().get(<span class="string">"k1"</span>));</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="四、三个基本缓存注解"><a href="#四、三个基本缓存注解" class="headerlink" title="四、三个基本缓存注解"></a>四、三个基本缓存注解</h2><h4 id="1、-Cacheable"><a href="#1、-Cacheable" class="headerlink" title="1、@Cacheable"></a>1、@Cacheable</h4><ul>
<li><p><code>@Cacheable</code>可以标记在一个方法上，也可以标记在一个类上。当标记在一个方法上时表示该方法是支持缓存的，当标记在一个类上时则表示该类所有的方法都是支持缓存的。对于一个支持缓存的方法，Spring会在其被调用后将其返回值缓存起来，以保证下次利用同样的参数来执行该方法时可以直接从缓存中获取结果，而不需要再次执行该方法。</p>
</li>
<li><p>判断缓存中如果存在则直接获取，不存在则新增</p>
<h4 id="2、-CachePut"><a href="#2、-CachePut" class="headerlink" title="2、@CachePut"></a>2、@CachePut</h4></li>
<li><p><code>@CachePut</code>可以声明一个方法支持缓存功能。与<code>@Cacheable</code>不同的是使用<code>@CachePut</code>标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果，而是每次都会执行该方法，并将执行结果以键值对的形式存入指定的缓存中。</p>
</li>
<li><p>不判断是否新增，每次都将查询到的数据存入缓存中 </p>
</li>
</ul>
<h4 id="3、-CacheEvict"><a href="#3、-CacheEvict" class="headerlink" title="3、@CacheEvict"></a>3、@CacheEvict</h4><ul>
<li><p><code>@CacheEvict</code>是用来标注在需要清除缓存元素的方法或类上的。当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作。<code>@CacheEvict</code>可以指定的属性有<code>value、key、condition、allEntries和beforeInvocation</code>。</p>
</li>
<li><p>每次执行都会删除指定的缓存</p>
<h4 id="三者共有属性"><a href="#三者共有属性" class="headerlink" title="三者共有属性"></a>三者共有属性</h4></li>
<li><p><strong>value(cacheName)</strong><br><code>@Cacheable、@CachePut</code>标注时表示生成的缓存的名称，<code>@CacheEvict</code>标注时表示将要清空的缓存的名称。</p>
</li>
<li><p><strong>key</strong><br>在同一名称（类别）下的缓存，需要唯一的key值来标识唯一的缓存，如未指定则会使用默认策略生成的key。</p>
</li>
<li><p><strong>condition</strong><br>表示缓存操作发生的条件。</p>
</li>
</ul>
<h4 id="CacheEvict的allEntries和beforeInvocation属性"><a href="#CacheEvict的allEntries和beforeInvocation属性" class="headerlink" title="@CacheEvict的allEntries和beforeInvocation属性"></a>@CacheEvict的allEntries和beforeInvocation属性</h4><ul>
<li><p><strong>allEntries</strong><br>  allEntries是boolean类型，表示是否需要清除同一名称的缓存中的所有元素。默认为false，表示不需要。当指定了allEntries为true时，Spring Cache将忽略指定的key，清除所有同一名称的缓存。</p>
</li>
<li><p><strong>beforeInvocation</strong><br>  清除操作默认是在对应方法成功执行之后触发的，即方法如果因为抛出异常而未能成功返回时也不会触发清除操作。使用beforeInvocation可以改变触发清除操作的时间，当我们指定该属性值为true时，Spring会在调用该方法之前清除缓存中的指定元素。</p>
</li>
</ul>
<h4 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Cacheable</span>(cacheNames = <span class="string">"User"</span>, key = <span class="string">"#userId"</span>)</span><br><span class="line"><span class="meta">@GetMapping</span>(<span class="string">"/testCacheable/&#123;userId&#125;"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> User <span class="title">testCacheable</span><span class="params">(@PathVariable(<span class="string">"userId"</span>)</span> Integer userId) </span>&#123;</span><br><span class="line">  <span class="comment">//方法体</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="五、demo地址"><a href="#五、demo地址" class="headerlink" title="五、demo地址"></a>五、demo地址</h2><blockquote>
<p><a href="https://gitee.com/fengzxia/spring-boot-redis-cache" target="_blank" rel="noopener">https://gitee.com/fengzxia/spring-boot-redis-cache</a>  </p>
</blockquote>
<p>实体类等内容不再展示， 这里只展示使用缓存注解的controller代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.lx.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.lx.dao.UserDao;</span><br><span class="line"><span class="keyword">import</span> com.lx.entity.User;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.cache.annotation.CacheEvict;</span><br><span class="line"><span class="keyword">import</span> org.springframework.cache.annotation.CachePut;</span><br><span class="line"><span class="keyword">import</span> org.springframework.cache.annotation.Cacheable;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.GetMapping;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.PathVariable;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.PostMapping;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RestController;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 用户操作</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> 段誉</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@create</span> 2019-04-11 9:52</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> </span>&#123;</span><br><span class="line">  <span class="meta">@Autowired</span></span><br><span class="line">  <span class="keyword">private</span> UserDao userDao;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@PostMapping</span>(<span class="string">"user"</span>)</span><br><span class="line">  <span class="function"><span class="keyword">public</span> String <span class="title">userAdd</span><span class="params">(User user)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> userDao.userInsert(user) != <span class="number">0</span> ? <span class="string">"success"</span> : <span class="string">"fail"</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@desc</span> 测试CachePut注解</span></span><br><span class="line"><span class="comment">   * 缓存名字为"User","userId"作为key</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@Author</span> 段誉</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@Date</span> 2019/4/11 9:56</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@method</span></span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="meta">@Cacheable</span>(cacheNames = <span class="string">"User"</span>, key = <span class="string">"#userId"</span>)</span><br><span class="line">  <span class="meta">@GetMapping</span>(<span class="string">"/testCacheable/&#123;userId&#125;"</span>)</span><br><span class="line">  <span class="function"><span class="keyword">public</span> User <span class="title">testCacheable</span><span class="params">(@PathVariable(<span class="string">"userId"</span>)</span> Integer userId) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> userDao.userQuery(userId);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@desc</span> 缓存名字为"User","userId"作为key</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@Author</span> 段誉</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@Date</span> 2019/4/11 9:56</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@method</span></span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="meta">@CachePut</span>(cacheNames = <span class="string">"User"</span>, key = <span class="string">"#userId"</span>)</span><br><span class="line">  <span class="meta">@GetMapping</span>(<span class="string">"/testCachePut/&#123;userId&#125;"</span>)</span><br><span class="line">  <span class="function"><span class="keyword">public</span> User <span class="title">testCachePut</span><span class="params">(@PathVariable(<span class="string">"userId"</span>)</span> Integer userId) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> userDao.userQuery(userId);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@desc</span> 测试CacheEvict注解清空指定用户缓存</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@Author</span> 段誉</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@Date</span> 2019/4/11 9:56</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@method</span></span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="meta">@CacheEvict</span>(cacheNames = <span class="string">"User"</span>, key = <span class="string">"#userId"</span>)</span><br><span class="line">  <span class="meta">@GetMapping</span>(<span class="string">"/testCacheEvict/&#123;userId&#125;"</span>)</span><br><span class="line">  <span class="function"><span class="keyword">public</span> String <span class="title">testCacheEvict</span><span class="params">(@PathVariable(<span class="string">"userId"</span>)</span> Integer userId) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">"cache for "</span> + userId + <span class="string">" has been flushed"</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@desc</span> 测试CacheEvict注解的allEntries属性清空所有用户缓存</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@Author</span> 段誉</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@Date</span> 2019/4/11 9:56</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@method</span></span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="meta">@CacheEvict</span>(cacheNames = <span class="string">"User"</span>, allEntries = <span class="keyword">true</span>)</span><br><span class="line">  <span class="meta">@GetMapping</span>(<span class="string">"/testAllEntries"</span>)</span><br><span class="line">  <span class="function"><span class="keyword">public</span> String <span class="title">testAllEntries</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">"All caches have been flushed"</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li><strong>需要在<code>Application.class</code>类上添加<code>@EnableCaching</code>注解后才能使配置的缓存生效</strong></li>
</ul>
<h3 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h3><p><strong>返回的对象必须要有一个无参构造函数才能正常返回结果否则报如下错误</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[&#x2F;].[dispatcherServlet]:</span><br><span class="line">175 - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; </span><br><span class="line">nested exception is org.springframework.data.redis.serializer.SerializationException: Could not read JSON: </span><br><span class="line">Cannot construct instance of &#96;com.leimo.response.Result&#96; (no Creators, like default construct, exist): </span><br><span class="line">cannot deserialize from Object value (no delegate- or property-based Creator)</span><br></pre></td></tr></table></figure>

<h2 id="六、CacheManager的定制"><a href="#六、CacheManager的定制" class="headerlink" title="六、CacheManager的定制"></a>六、CacheManager的定制</h2><h3 id="1、未定制前"><a href="#1、未定制前" class="headerlink" title="1、未定制前"></a>1、未定制前</h3><p>当我们使用引入redis的starter时，容器中默认使用的是<code>RedisCacheManager</code>。它在操作redis时使用的是<code>RedisTemplate&lt;Object, Object&gt;</code>,默认采用JDK的序列化机制，例如redis中查看用户的缓存：<br><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9pLmxvbGkubmV0LzIwMTkvMDQvMTEvNWNhZWFkNDU3ZjFhNi5wbmc" alt="UTOOLS1554951491908.png"></p>
<p>我们可以通过定制CacheManager改变采取的序列化机制。</p>
<h3 id="2、进行定制"><a href="#2、进行定制" class="headerlink" title="2、进行定制"></a>2、进行定制</h3><h4 id="配置类"><a href="#配置类" class="headerlink" title="配置类"></a>配置类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.redis.cache.RedisCacheConfiguration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.redis.cache.RedisCacheManager;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.redis.connection.RedisConnectionFactory;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.redis.serializer.RedisSerializationContext;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RedisConfig</span> </span>&#123;</span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@desc</span> 序列化缓存定制</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@Author</span> 段誉</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@Date</span> 2019/4/11 13:01</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@method</span></span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  <span class="meta">@Bean</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> RedisCacheManager <span class="title">jsonCacheManager</span><span class="params">(RedisConnectionFactory factory)</span> </span>&#123;</span><br><span class="line">    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()</span><br><span class="line">            .serializeValuesWith(RedisSerializationContext.SerializationPair</span><br><span class="line">                .fromSerializer(<span class="keyword">new</span> GenericJackson2JsonRedisSerializer()));</span><br><span class="line">    <span class="keyword">return</span> RedisCacheManager.builder(factory)</span><br><span class="line">            .cacheDefaults(config)</span><br><span class="line">            .build();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>缓存之后查看<br><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9pLmxvbGkubmV0LzIwMTkvMDQvMTEvNWNhZWNkNTMwM2IwNy5wbmc" alt="UTOOLS1554959697256.png"></p>
<p>到此，已经完成Json的序列化。其他配置可根据<code>RedisCacheConfiguration</code>中的不同方法配置。</p>

<div id="gitalk-container"></div>
<script src="https://cdn.bootcss.com/blueimp-md5/2.12.0/js/md5.min.js"></script><link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css"><script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>

		<script>
		var gitalkConfig = {"clientID":"28a065e3b5afee156f4f","clientSecret":"e18104d6f3219526491fc42c892ff6a26b8f3e74","repo":"blog_comment","owner":"FENG1000","admin":["FENG1000"],"distractionFreeMode":false};
	    gitalkConfig.id = md5(location.pathname);
		var gitalk = new Gitalk(gitalkConfig);
	    gitalk.render("gitalk-container");
	    </script>
    
  </div>
</article>




        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">主页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/categories/">分类</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/about/">关于</a></li>
         
          <li><a href="/friend_ship/">友链</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一、redis简介"><span class="toc-number">1.</span> <span class="toc-text">一、redis简介</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1、概述"><span class="toc-number">1.1.</span> <span class="toc-text">1、概述</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2、优点"><span class="toc-number">1.2.</span> <span class="toc-text">2、优点</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二、redis的搭建"><span class="toc-number">2.</span> <span class="toc-text">二、redis的搭建</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1、安装"><span class="toc-number">2.1.</span> <span class="toc-text">1、安装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2、测试"><span class="toc-number">2.2.</span> <span class="toc-text">2、测试</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#三、基本环境配置"><span class="toc-number">3.</span> <span class="toc-text">三、基本环境配置</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1、pom依赖"><span class="toc-number">3.0.1.</span> <span class="toc-text">1、pom依赖</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2、yml配置"><span class="toc-number">3.0.2.</span> <span class="toc-text">2、yml配置</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3、测试连接"><span class="toc-number">3.0.3.</span> <span class="toc-text">3、测试连接</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#四、三个基本缓存注解"><span class="toc-number">4.</span> <span class="toc-text">四、三个基本缓存注解</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1、-Cacheable"><span class="toc-number">4.0.1.</span> <span class="toc-text">1、@Cacheable</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2、-CachePut"><span class="toc-number">4.0.2.</span> <span class="toc-text">2、@CachePut</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3、-CacheEvict"><span class="toc-number">4.0.3.</span> <span class="toc-text">3、@CacheEvict</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#三者共有属性"><span class="toc-number">4.0.4.</span> <span class="toc-text">三者共有属性</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#CacheEvict的allEntries和beforeInvocation属性"><span class="toc-number">4.0.5.</span> <span class="toc-text">@CacheEvict的allEntries和beforeInvocation属性</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#实例"><span class="toc-number">4.0.6.</span> <span class="toc-text">实例</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#五、demo地址"><span class="toc-number">5.</span> <span class="toc-text">五、demo地址</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#注意"><span class="toc-number">5.1.</span> <span class="toc-text">注意</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#六、CacheManager的定制"><span class="toc-number">6.</span> <span class="toc-text">六、CacheManager的定制</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1、未定制前"><span class="toc-number">6.1.</span> <span class="toc-text">1、未定制前</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2、进行定制"><span class="toc-number">6.2.</span> <span class="toc-text">2、进行定制</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#配置类"><span class="toc-number">6.2.1.</span> <span class="toc-text">配置类</span></a></li></ol></li></ol></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://fengzxia.gitee.io/posts/90da3b9e.html" target="_blank" rel="noopener"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://fengzxia.gitee.io/posts/90da3b9e.html&text=SpringBoot+Redis作为二级缓存整合" target="_blank" rel="noopener"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://fengzxia.gitee.io/posts/90da3b9e.html&title=SpringBoot+Redis作为二级缓存整合" target="_blank" rel="noopener"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://fengzxia.gitee.io/posts/90da3b9e.html&is_video=false&description=SpringBoot+Redis作为二级缓存整合" target="_blank" rel="noopener"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=SpringBoot+Redis作为二级缓存整合&body=Check out this article: https://fengzxia.gitee.io/posts/90da3b9e.html"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=https://fengzxia.gitee.io/posts/90da3b9e.html&title=SpringBoot+Redis作为二级缓存整合" target="_blank" rel="noopener"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://fengzxia.gitee.io/posts/90da3b9e.html&title=SpringBoot+Redis作为二级缓存整合" target="_blank" rel="noopener"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://fengzxia.gitee.io/posts/90da3b9e.html&title=SpringBoot+Redis作为二级缓存整合" target="_blank" rel="noopener"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://fengzxia.gitee.io/posts/90da3b9e.html&title=SpringBoot+Redis作为二级缓存整合" target="_blank" rel="noopener"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://fengzxia.gitee.io/posts/90da3b9e.html&name=SpringBoot+Redis作为二级缓存整合&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=https://fengzxia.gitee.io/posts/90da3b9e.html&t=SpringBoot+Redis作为二级缓存整合" target="_blank" rel="noopener"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> 菜单</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> 目录</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> 分享</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> 返回顶部</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy;
    
    
    2019-2020
    只是学习学习
  </div>
  <div class="footer-right">
    <nav>
      <ul>
         
          <li><a href="/">主页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/categories/">分类</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/about/">关于</a></li>
         
          <li><a href="/friend_ship/">友链</a></li>
        
      </ul>
    </nav>
  </div>
</footer>

    </div>
    <!-- styles -->

<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">


<link rel="stylesheet" href="/lib/justified-gallery/css/justifiedGallery.min.css">


    <!-- jquery -->

<script src="/lib/jquery/jquery.min.js"></script>


<script src="/lib/justified-gallery/js/jquery.justifiedGallery.min.js"></script>

<!-- clipboard -->

  
<script src="/lib/clipboard/clipboard.min.js"></script>

  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"复制到粘贴板!\">";
    btn += '<i class="far fa-clone"></i>';
    btn += '</span>'; 
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "复制成功!");
      e.clearSelection();
    })
  })
  </script>


<script src="/js/main.js"></script>

<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

    <script type="text/javascript">
        var _hmt = _hmt || [];
        (function() {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?54382acbd8c4bc66535bce135e0ac166";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
        })();
    </script>

<!-- Disqus Comments -->


</body>
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https'){
   bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
  }
  else{
  bp.src = 'http://push.zhanzhang.baidu.com/push.js';
  }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>
</html>
